"""
kafka消费者代码Demo
"""
from pykafka import KafkaClient
from app.database import get_db
from app.cache import get_redis


def consumer_fun():
    client = KafkaClient(hosts='10.0.42.160:9092')
    topic = client.topics['testTopic001'.encode()]
    consumer = topic.get_simple_consumer(
        consumer_group="group1",
        auto_commit_enable=True,
        consumer_id="user1")
    db = get_db()
    rd = get_redis()

    for msg in consumer:
        if msg is not None:
            print(msg.offset, msg.value)
            db.query("insert into xxx values(1,2,3)")
            rd.set("AAAA", 1)


if __name__ == '__main__':
    consumer_fun()
